package com.amit.hibernate;

import java.io.Serializable;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

public class HibernateTemplate {

	public static Object saveObject(Customer cust) {
		
		Object id=null;
		
		try
		{
			SessionFactory sessionfactory=ChibernateUtil.getfactory();
			Session session=sessionfactory.openSession();
			Transaction tx=session.beginTransaction();
			id=session.save(cust);
			tx.commit();
			session.close();
		}
		catch (Exception e) {
			e.printStackTrace();
			
			
		}
		
		return id;
	}

	public static Object loadObject(Class cls, Serializable s) {
		Object o=null;
		
		try
		{
			SessionFactory sessionfactory=ChibernateUtil.getfactory();
			Session session=sessionfactory.openSession();
			Transaction tx=session.beginTransaction();
	    
			o=session.load(cls, s);
			
			tx.commit();
			
			session.close();
		}
		catch (Exception e) {
			
			e.printStackTrace();
			
		}
		return o;
	}

	public static void deleteObject(Class cls, Serializable s)
	{

		try
		{
			SessionFactory sessionfactory=ChibernateUtil.getfactory();
			Session session=sessionfactory.openSession();
			Transaction tx=session.beginTransaction();
	    
		 Object o=session.load(cls, s);
		 session.delete(o);
			
			tx.commit();
			
			session.close();
		}
		catch (Exception e) {
			
			e.printStackTrace();
			
		}	
		
	}

	public static void updateObject(Customer cust) {
		

		try
		{
			SessionFactory sessionfactory=ChibernateUtil.getfactory();
			Session session=sessionfactory.openSession();
			Transaction tx=session.beginTransaction();
	    
		 
		 session.update(cust);
			
			tx.commit();
			
			session.close();
		}
		catch (Exception e) {
			
			e.printStackTrace();
			
		}
	}

}
